package com.slz3.business.module.goodsManagement.gooInWarehouse.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.goodsManagement.gooInWarehouse.domain.GooInWarehouse;
import com.slz3.business.module.goodsManagement.gooInWarehouse.domain.GooInWarehouseFactory;
import com.slz3.business.module.goodsManagement.gooInWarehouse.steward.GooInWarehouseSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class GooInWarehouseServiceImpl implements GooInWarehouseService {
    @Override
    public GooInWarehouse save(GooInWarehouse gooInWarehouse) {
        return Repositorys.gooInWarehouse.save(GooInWarehouseFactory.initBaseFields(gooInWarehouse));
    }

    @Override
    public Page<GooInWarehouse> page(MultiValueMap<String, String> params) {
        Page<GooInWarehouse> gooInWarehousePage = Repositorys.gooInWarehouse.findAll(
                GooInWarehouseSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return gooInWarehousePage;
    }


    @Override
    public GooInWarehouse modify(GooInWarehouse gooInWarehouseNew) {
        return Repositorys.gooInWarehouse.saveAndFlush(GooInWarehouseFactory.initBaseFields(gooInWarehouseNew));
    }

    @Override
    public Optional<GooInWarehouse> single(UUID uuid) {
        return Repositorys.gooInWarehouse.findById(uuid);
    }

    @Override
    public List<GooInWarehouse> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.gooInWarehouse.findAllById(uuids);
    }

    @Override
    public List<GooInWarehouse> delete(List<GooInWarehouse> gooInWarehouseList) {
        gooInWarehouseList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            GooInWarehouseFactory.initBaseFields(item);
        });
        return Services.gooInWarehouseTransactional.batchModify(gooInWarehouseList);
    }

}
